using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Controls;
using CommunityServer.Components;

namespace CommunityServerStuff.Controls
{
    public class FilteredRoleDropDownList : DropDownList
    {

        #region Properties

        #region ExcludeAllBuiltInRoles

        /// <summary>
        /// Automatically removes all of the built-in system roles (SystemAdministrator, Owners, Registered Users, etc).
        /// </summary>
        public bool ExcludeBuiltInRoles
        {
            get { return (bool)(ViewState["ExcludeBuiltInRoles"] ?? false); }
            set
            {
                ViewState["ExcludeBuiltInRoles"] = value;
                //if (value)
                //{
                //    if (_excludedRoles == null)
                //        _excludedRoles = new List<string>();
                //    _excludedRoles.AddRange(administrators.Split(splitter, System.StringSplitOptions.RemoveEmptyEntries));
                //    _excludedRoles.AddRange(systemRoles.Split(splitter, System.StringSplitOptions.RemoveEmptyEntries));
                //}
            }
        }

        #endregion

        #region ExcludeAdministrativeRoles

        /// <summary>
        /// Automatically removes the built-in administrative roles (SystemAdministrator, etc).
        /// </summary>
        public bool ExcludeAdministrativeRoles
        {
            get { return (bool)(ViewState["ExcludeAdministrativeRoles"] ?? false); }
            set
            {
                ViewState["ExcludeAdministrativeRoles"] = value;
                //if (value)
                //{
                //    if (_excludedRoles == null)
                //        _excludedRoles = new List<string>();
                //    _excludedRoles.AddRange(administrators.Split(splitter, System.StringSplitOptions.RemoveEmptyEntries));
                //}
            }
        }

        #endregion

        #region ExcludeNonAdministrativeRoles

        /// <summary>
        /// Automatically removes all of the built-in system roles (Owners, Registered Users, etc).
        /// </summary>
        public bool ExcludeNonAdministrativeRoles
        {
            get { return (bool)(ViewState["ExcludeNonAdministrativeRoles"] ?? false); }
            set
            {
                ViewState["ExcludeNonAdministrativeRoles"] = value;
                //if (value)
                //{
                //    if (_excludedRoles == null)
                //        _excludedRoles = new List<string>();
                //    _excludedRoles.AddRange(systemRoles.Split(splitter, System.StringSplitOptions.RemoveEmptyEntries));
                //}
            }
        }

        #endregion

        #endregion

        #region Methods

        #region Constructor

        public FilteredRoleDropDownList()
        {
            this.DataBound += this.RemoveRoles;
        }

        #endregion

        #region RemoveRoles

        public void RemoveRoles(object sender, EventArgs e)
        {
            List<ListItem> removedItems = new List<ListItem>();
            foreach (ListItem li in this.Items)
            {
                if ((this.ExcludeBuiltInRoles) && (Roles.IsBuiltInRole(li.Text) || li.Text.Equals("ReaderUser") || li.Text.Equals("Trusted Users")))
                    removedItems.Add(li);
                
            }

            foreach (ListItem li in removedItems)
            {
                    this.Items.Remove(li);
            }

        }

        #endregion

        #endregion
    }
}
